package com.example.xunua_zhinengjiaotong.Dialog;


import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.xunua_zhinengjiaotong.R;
import com.example.xunua_zhinengjiaotong.utils.HttpUtil;
import com.example.xunua_zhinengjiaotong.utils.MyApp;

import org.json.JSONObject;

public class BusDialog2 extends AlertDialog {
    private Context context;
    private int num;
    private TextView tv_title,tv1,tv2;
    private Button btn_chaxun;
    private Spinner spinner;
    private MyApp myApp;
    private HttpUtil util=new HttpUtil();

    public BusDialog2(int num, Context context, MyApp myApp) {
        super(context,R.style.myDialog);
        this.context=context;
        this.num=num;
        this.myApp=myApp;
    }

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    tv2.setText(msg.obj+"");
                    break;
                case -1:
                    tv2.setText("查询失败");
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_dialog_bus2);
        initView();
    }

    private void initView() {
        tv_title=findViewById(R.id.bus2_tv_title);
        tv1=findViewById(R.id.bus2_rl_tv1);
        tv2=findViewById(R.id.bus2_rl_tv2);
        btn_chaxun=findViewById(R.id.bus2_btn_chaxun);
        spinner=findViewById(R.id.bus2_spinner);
        spinner.setAdapter(new ArrayAdapter<>(context,R.layout.support_simple_spinner_dropdown_item,
                new String[]{"1号公交车","2号公交车"}));
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (num == 0) {
                    tv1.setText(spinner.getSelectedItem().toString()+"位置：");
                } else if (num == 1) {
                    tv1.setText(spinner.getSelectedItem().toString()+"速度：");
                    tv_title.setText("公交速度");
                } else if (num == 2) {
                    tv1.setText(spinner.getSelectedItem().toString()+"车载容量：");
                    tv_title.setText("公交车载容量");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        btn_chaxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (num == 0) {
                    //接口好像有问题。就不写了。
//                    Toast.makeText(context, "查询失败", Toast.LENGTH_SHORT).show();
                }
                if (num == 1) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject getBusSpeed = util.connectServer("{\"BusId\":" + (spinner.getSelectedItemId() + 1)+ "}", myApp, "GetBusSpeed");
                                String busSpeed = getBusSpeed.getString("BusSpeed");
                                Message message = new Message();
                                message.what=0;
                                message.obj=busSpeed;
                                handler.sendMessage(message);
                            }catch (Exception e){
                                handler.sendEmptyMessage(-1);
                                e.printStackTrace();
                            }
                        }
                    }).start();
                } else if (num == 2) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jsonObject = util.connectServer("{\"BusId\":" + (spinner.getSelectedItemId() + 1) + "}", myApp, "GetBusCapacity");
                                String busCapacity = jsonObject.getString("BusCapacity");
                                Message message = new Message();
                                message.what=0;
                                message.obj=busCapacity;
                                handler.sendMessage(message);
                            }catch (Exception e){
                                handler.sendEmptyMessage(-1);
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
            }
        });
    }
}
